package pl.elka.pobr.ppr.utils;

/**
 * HSV and RGB transformation
 */
public class ColorTransforms {

    public static double[] rgb2hsv(double red, double grn, double blu) {
        double hue = -1, sat = -1, val = -1;
        double result[] = new double[3];
        double temp = Math.min(Math.min(red, grn), blu);
        val = Math.max(Math.max(red, grn), blu);
        if (temp == val) {
            hue = 0;
        } else {
            if (red == val) {
                hue = 0 + ((grn - blu) * 60 / (val - temp));
            } else if (grn == val) {
                hue = 120 + ((blu - red) * 60 / (val - temp));
            } else {
                hue = 240 + ((red - grn) * 60 / (val - temp));
            }
        }
        if (hue < 0) {
            hue += 360;
        }
        if (val == 0) {
            sat = 0;
        } else {
            sat = (val - temp) * 100 / val;
        }
        val = (100 * val) / 255;
        result[0] = hue;
        result[1] = sat;
        result[2] = val;
        return result;
    }
}